<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        // 集合
        const A = new Set([1,2,3,4,5]);
        const B = new Set([4,5,6,7,8]);

        // 并集
        const union = [...new Set([...A, ...B])];
        // 交集
        const intersection = [...A].filter(x => B.has(x));
        // 差集
        const differenceAB = [...A].filter(x => !B.has(x)); // A - B
        const differenceBA = [...B].filter(x => !A.has(x)); // B - A

        
        console.log('并集:', union); // 输出: 并集: [1, 2, 3, 4, 5, 6, 7, 8]
        console.log('交集:', intersection); // 输出: 交集: [4, 5]
        console.log('差集 (A - B):', differenceAB); // 输出: 差集 (A - B): [1, 2, 3]
        console.log('差集 (B - A):', differenceBA); // 输出: 差集 (B - A): [6, 7, 8]
    </script>
</body>
</html>